package com.estronger.shopmanage.ui.install.pg;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.adapter.tasking.PgInstallDetailListAdapter;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.B34BuildDetailGoods;
import com.estronger.shopmanage.bean.InstallDPG;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.utils.PermissionUtils;
import com.estronger.shopmanage.view.AppDialog;
import com.estronger.shopmanage.view.CustomListView;
import com.lidroid.xutils.http.RequestParams;

import cn.jesse.nativelogger.NLogger;

/**
 * 安装任务详情
 * Created by Soonfor on 2017/5/17.
 */

public class InstallPGDetailActivity extends BaseActivity implements
        View.OnClickListener {

    private Activity mActivity;
    private View rootView;
    private ScrollView sv_root;
    private InstallDPG.Datas.InstallDPGListData itemData;
    private String installid;
    private TextView tv_title, tvfName, tvfAddress, tvfOrderTimeT, tvfOrderTime/* ,tvfSales*/;
    private ImageView ivfCallToCustomer, ivfSendToCustomer/*, ivfCallToSales, ivfSendToSales*/;
    private LinearLayout llfSales;
    private String cstName, cstphoneNumber, salesName, salesphoneNumber;
    private CustomListView goodsListView;
    private FloatingActionButton fab;
    private Dialog dialog;

    @Override
    protected void init() {
        itemData = (InstallDPG.Datas.InstallDPGListData) getIntent().getSerializableExtra("InstallItem");
        if(itemData != null){
            installid = itemData.dlvid;
        }
    }

    @Override
    protected void initView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mActivity = InstallPGDetailActivity.this;
        rootView = View.inflate(mActivity, R.layout.activity_pg_msdetail, null);
        setContentView(rootView);
        tv_title = (TextView) findViewById(R.id.ab_title);
        tv_title.setText("安装任务详情");
        sv_root = (ScrollView) findViewById(R.id.detail_pgscroller);

        RequestParams params = new RequestParams();
        params.addBodyParameter("sf_action", "getistinfo");
        params.addBodyParameter("usercode", MyApplication.getUserCode());
        params.addBodyParameter("dlvid", installid);
        connect(params, NetManager.DETAIL);
        fab = (FloatingActionButton) findViewById(R.id.fab);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mActivity, InstallPGTripActivity.class));
            }

            ;
        });
        initTitle(sv_root);
        goodsListView.setFocusable(false);
    }

    @Override
    protected void initAction() {
        super.initAction();
        setTextContent();
        goodsListView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                }
                return false;
            }
        });
    }

    private void initTitle(View root) {
        llfSales = (LinearLayout) root.findViewById(R.id.llfSales);
        tvfName = (TextView) root.findViewById(R.id.tvfItemTitle);
        tvfAddress = (TextView) root.findViewById(R.id.tvfItemLocation);
        // tvfSales = (TextView) root.findViewById(R.id.tvfSales);
        tvfOrderTimeT = (TextView) root.findViewById(R.id.tvfOrderTimeT);
        tvfOrderTime = (TextView) root.findViewById(R.id.tvfOrderTime);

        ivfCallToCustomer = (ImageView) root.findViewById(R.id.ivfMCall);
        //ivfCallToSales = (ImageView) root.findViewById(R.id.ivfMCallSales);
        ivfSendToCustomer = (ImageView) root.findViewById(R.id.ivfMSendms);
        //ivfSendToSales = (ImageView) root.findViewById(R.id.ivfMSendToSales);

        goodsListView = (CustomListView) root.findViewById(R.id.cLvfGoods);
        llfSales.setVisibility(View.GONE);
        ivfCallToCustomer.setOnClickListener(this);
        ivfSendToCustomer.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivfMSendms:
                if (isRightNumber(cstphoneNumber)) {
                    Uri uri = Uri.parse("smsto:" + cstphoneNumber);
                    Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
                    startActivity(intent);
                }
                break;
            case R.id.ivfMCall:
                if (isRightNumber(cstphoneNumber)) {
                    dialog = AppDialog.createDialog(this, cstName, cstphoneNumber, this, installid);
                    dialog.show();
                }
                break;
            case R.id.ok:
                try {
                    if (PermissionUtils.isCallPhoneGranted(mActivity)) {
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
                                + salesphoneNumber));
                        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            //    ActivityCompat#requestPermissions
                            // here to request the missing permissions, and then overriding
                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                            //                                          int[] grantResults)
                            // to handle the case where the user grants the permission. See the documentation
                            // for ActivityCompat#requestPermissions for more details.
                            return;
                        }
                        startActivity(intent);

                    } else {
                        PermissionUtils.ToSetPermissions(mActivity, R.string.PermissionsCallPhone);
                    }
                } catch (Exception e) {
                    NLogger.e("" + e.getMessage());
                }
                dialog.dismiss();
                break;
        }
    }

    /**
     * 当前activity请求网络成功的回调
     *
     * @params code 各个接口返回的唯一识别码，用来判断是哪个接口成功返回
     * @params result 网络请求成功返回的数据
     */
    @Override
    protected void onNetSuccess(int code, String result) {
        super.onNetSuccess(code, result);
        if (NetManager.DETAIL == code) {
            showCompleshView(result);
        }
    }

    //本Activity中请求的数据
    public void showCompleshView(String result) {
        B34BuildDetailGoods pdetail = gson.fromJson(result, B34BuildDetailGoods.class);
        B34BuildDetailGoods.DetailGoods pData = pdetail.data;
        if (pdetail == null || !pdetail.success) {
            return;
        }
        PgInstallDetailListAdapter pdAdapter = new PgInstallDetailListAdapter(mActivity, pData.istitem);
        goodsListView.setAdapter(pdAdapter);
    }
    //上一个Activity带入的数据
    private void setTextContent(){

        if(itemData == null){return;}
        if (TextUtils.isEmpty(itemData.cstbuildname)) {
            cstName = itemData.cstname;
        } else {
            cstName = itemData.cstbuildname + "-" + itemData.cstname;
        }
        tvfName.setText(cstName);
        tvfAddress.setText(itemData.address);
        tvfOrderTimeT.setText("预计安装时间:");
        tvfOrderTime.setText(itemData.pidate);
        cstphoneNumber = itemData.cstmobile;
    }
}
